﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Kinect;

namespace ZeroTouchUI
{
    /// <summary>
    /// This control is used to show the recorded
    /// gesture in the AddGesture window.
    /// </summary>
    public class GestureCanvas : Control
    {
        public Gesture gesture;

        public GestureCanvas()
        {
            gesture = new Gesture();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            if (gesture != null)
            {
                Brush rightHandBrush = Brushes.Red;
                Brush leftHandBrush = Brushes.Blue;
                Pen rightGesturePen = new Pen(Brushes.Red, 0.5);
                Pen leftGesturePen = new Pen(Brushes.Blue, 0.5);

                if (gesture.rightGesture != null)
                {
                    double pThickness = 0.5;
                    double lThickness = 0.5;
                    List<Point> newPoints = new List<Point>();
                    foreach (Point point in gesture.rightGesture)
                    { 
                        newPoints.Add(new Point { X = point.X * (double)(100.0 / 250.0), Y = point.Y * (double)(100.0 / 250.0) });
                    }

                    foreach (Point point in newPoints)
                    {
                        rightGesturePen.Thickness = lThickness;
                        drawingContext.DrawEllipse(rightHandBrush, null, point, pThickness, pThickness);
                        if (newPoints.IndexOf(point) != 0)
                        {
                            drawingContext.DrawLine(rightGesturePen, point, newPoints[newPoints.IndexOf(point) - 1]);
                        }
                        pThickness += 0.1;
                        lThickness += 0.1;
                    }
                }
                if (gesture.leftGesture != null)
                {
                    double pThickness = 0.5;
                    double lThickness = 0.5;
                    List<Point> newPoints = new List<Point>();
                    foreach (Point point in gesture.leftGesture)
                    {
                        newPoints.Add(new Point { X = point.X * (double)(100.0 / 250.0), Y = point.Y * (double)(100.0 / 250.0) });
                    }

                    foreach (Point point in newPoints)
                    {
                        leftGesturePen.Thickness = lThickness;
                        drawingContext.DrawEllipse(leftHandBrush, null, point, pThickness, pThickness);
                        if (newPoints.IndexOf(point) != 0)
                        {
                            drawingContext.DrawLine(leftGesturePen, point, newPoints[newPoints.IndexOf(point) - 1]);
                        }
                        pThickness += 0.1;
                        lThickness += 0.1;
                    }
                }
            }
        }
    }
}
